#Boa:Dialog:Dialog1

import wx
import consultasBD
import utiles

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1LISTBOX1, wxID_DIALOG1PANEL1, 
 wxID_DIALOG1STATICTEXT1, wxID_DIALOG1TEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(6)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(511, 89), size=wx.Size(305, 438),
              style=wx.DEFAULT_DIALOG_STYLE, title='Buscar Proveedor')
        self.SetClientSize(wx.Size(297, 411))

        self.panel1 = wx.Panel(id=wxID_DIALOG1PANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(384, 416),
              style=wx.TAB_TRAVERSAL)

        self.textCtrl1 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL1, name='textCtrl1',
              parent=self.panel1, pos=wx.Point(8, 24), size=wx.Size(200, 21),
              style=0, value='')

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Nombre del proveedor', name='staticText1',
              parent=self.panel1, pos=wx.Point(8, 8), size=wx.Size(107, 13),
              style=0)

        self.button1 = wx.Button(id=wxID_DIALOG1BUTTON1, label='Buscar',
              name='button1', parent=self.panel1, pos=wx.Point(216, 24),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnBotonBuscar,
              id=wxID_DIALOG1BUTTON1)

        self.listBox1 = wx.ListBox(choices=[], id=wxID_DIALOG1LISTBOX1,
              name='listBox1', parent=self.panel1, pos=wx.Point(8, 56),
              size=wx.Size(280, 344), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListSeleccion,
              id=wxID_DIALOG1LISTBOX1)

    def __init__(self, parent,nombre=""):
        self._init_ctrls(parent)
        self.eleccion = ""
        if nombre != "":
            self.textCtrl1.SetValue(nombre)
            self.buscandoProveedor()
            
    def buscandoProveedor(self):
        if self.textCtrl1.GetValue() == "*":
            sql = "SELECT idproveedor,nombre FROM proveedor WHERE 1 ORDER BY nombre"
        else:
            sql = "SELECT idproveedor,nombre FROM proveedor WHERE nombre LIKE '%"+self.textCtrl1.GetValue()+"%' ORDER BY nombre"
        resultados = consultasBD.ordenCruda(sql)
        if len(resultados)==1:
            self.eleccion = resultados[0][1][:30]+" / "+resultados[0][0]
            self.Close()
        self.listBox1.Clear()
        for item in resultados:
            aux1 = item[0]   #idproveedor
            aux2 = item[1][:30]   #nombre
            cadena = aux2+" / "+aux1
            self.listBox1.Append(cadena)

    def OnBotonBuscar(self, event):
        self.buscandoProveedor()

    def OnListSeleccion(self, event):
        self.eleccion = self.listBox1.GetStringSelection()
        self.Close()
        
    def getSeleccion(self):
        return self.eleccion

